<?php
/**
 * Created by PhpStorm.
 * User: 张鹏
 * Date: 2017-03-30
 * Time: 下午 8:28
 * Versions：v1.02
description:
 * 2017-05-05 新增方法 getOrderidByUserid 根据user_id 获取 order_id
 * 2017-05-05 新增方法 userOrderList 根据order_id 获取客户充值订单
 * 2017-05-15 新增方法 consume($filter) 用户消费及充值情况
 * 2017-05-17 更新方法 userOrderList
 */
namespace app\hongyanche\model;


use think\Db;

class Client
{
    //客户交易记录查询
    public function getOrderidByUserid($filter)
    {
        //检查权限
        $admin = model('admin');
        $auth = $admin->is('client');
        if (!$auth['state']) {
            return ['state'=>0,'errormsg'=>'authority incorrect','errorcode'=>'client75889'];
        }
        if (!isset($filter['user_id'])) {
            return ['state' => 0, 'not user exist'];
        }
        if (isset($filter['page']) && $filter['page'] != '') {
            $pages = $filter['page'] - 1;
        } else {
            $pages = 0;
        }
        if (isset($filter['maxPerPage']) && $filter['maxPerPage'] != '') {
            $maxPerPage = $filter['maxPerPage'];
        } else {
            $maxPerPage = 12;
        }
        $order = Db::name('pay')
            ->alias('p')
            ->join('order o', 'o.order_id = p.order_id')
            ->where("o.user_id=" . $filter['user_id'])
            ->order('o.create_time desc')
            ->field('o.order_id')
            ->limit($pages * $maxPerPage, $maxPerPage)
            ->select();
        $order_id = '';
        foreach ($order as $k => $v) {
            $order_id .= $v['order_id'] . ',';
        }
        $order_id = rtrim($order_id, ',');
        if (!$order) {
            return array('state' => 1, 'data' => array('list' => []));
        }
        $pages = $pages + 1;
        $order2 = Db::name('pay')
            ->alias('p')
            ->join('order o', 'o.order_id = p.order_id')
            ->where("o.user_id=" . $filter['user_id'])
            ->order('o.create_time desc')
            ->field('o.order_id')
            ->limit($pages * $maxPerPage, $maxPerPage)
            ->select();
        if ($order2) {
            $more = 1;
        } else {
            $more = 0;
        }
        return array('state' => 1, 'data' => array('list' => $order_id, 'more' => $more));
    }
    //客户充值订单
    public function userOrderList($orders_id){
        if(!$orders_id){
            return ['state'=>0,'need order_id'];
        }
        $order =Db::name('order')->where("class = 0 and order_id in($orders_id)")->order('create_time desc')->select();
        $orderlist = [];
        foreach ($order as $key=>$value){
            $tel = model('user')->getInfoById($value['user_id']);
            $orderlist[$key]['tel'] = isset($tel['tel']) ? $tel['tel'] : "";
            $orderlist[$key]['create_time'] = $value['create_time'];
            $orderlist[$key]['total_fee'] = $value['total_fee'];
            $orderlist[$key]['order_id'] = $value['order_id'];
            $orderlist[$key]['user_id'] = $value['user_id'];
            $orderlist[$key]['class'] = 'recharge';
            $user_info = model('user')->getInfoById($value['user_id']);
            $orderlist[$key]['user_name'] = isset($user_info['set_name']) ? $user_info['set_name'] : userTextDecode($user_info['user_name']);
            //计算充值退款金额
            $order_id = $value['order_id'];
            $pay_refund = Db::name('pay_refund')->where("order_id = '$order_id'")->select();
            $refund_total_fee = 0;
            if (isset($pay_refund)&&$pay_refund){
                foreach ($pay_refund as $k => $v) {
                    $refund_total_fee += $v['refund_fee'];
                }
            }
            //充值订单退款
            $orderlist[$key]['refund_fee'] = $refund_total_fee;
            $pay = Db::name('pay')->where("order_id = $value[order_id]")->find();
            if($pay){
                if (isset($pay['channel'])) {
                    switch ($pay['channel']) {
                        case 1:
                            $orderlist[$key]['cashpay'] = 1;
                            break;
                        case 2:
                            $orderlist[$key]['accountpay'] = 1;
                            break;
                        default:
                            switch ((int)($pay['channel'] / 100)) {
                                case 1:
                                    $orderlist[$key]['wxpay'] = 1;
                                    break;
                                case 2:
                                    $orderlist[$key]['alipay'] = 1;
                                    break;
                            }
                    }
                }
            }else{
                $orderlist[$key]['overdue'] = 1;
                /*$config = getConfig('product');
                if($config['cafe']['pay_in']<time()-$value['create_time']){

                }else{
                    $orderlist[$key]['unpaid'] = 1;
                }*/
            }
        }
        return ['state'=>1,'data'=>['list'=>$orderlist]];

    }
    //客户消费情况
    public function consume($filter){
        if (!$filter['user_id']){
            return ['state'=>0,'errormsg'=>'not find user_id','errorcode'=>'client consume101'];
        }
        $filter['page'] = input('page');
        $filter['maxPerPage'] = 12;
        $filter['keywords'] = input('keywords');
        //获取积分
        $ponits=db('forum_points')->field('sum(amount) as amount')->where("user_id = $filter[user_id]")->select()[0]['amount'];
        $list['points'] = $ponits?$ponits:0;
        $empirical_value = db('forum_points')->field('sum(amount) as amount')->where("user_id = $filter[user_id] and amount>0")->select()[0]['amount'];
        //判断等级
        if($empirical_value<50){
            $list['empirical_value'] = 'LV1';
        }elseif($empirical_value>=50 && $empirical_value<300){
            $list['empirical_value'] = 'LV2';
        }elseif (($empirical_value>=300 && $empirical_value<500)){
            $list['empirical_value'] = 'LV3';
        }elseif ($empirical_value>=500 && $empirical_value<5000){
            $list['empirical_value'] = 'LV4';
        }elseif ($empirical_value>=5000){
            $list['empirical_value'] = 'LV5';
        }
        $list['balance'] = model('Account')->getBalanceByUserId($filter['user_id'])['data']['balance'];
        $order_id = model('order')->getOrderidByUserid($filter);
        if(!$order_id['state']) return array('data'=>['info'=>$list],'state'=>1);
        if(!$order_id['data']['list']) return array('data'=>['info'=>$list],'state'=>1);
        $clientOrder = model('client')->userOrderList($order_id['data']['list'],$filter['user_id']);
        if($clientOrder['state']) $clientOrder = $clientOrder['data']['list'];
        $order = $clientOrder;
        $list['recharge_total_fee'] = 0;
        $list['recharge_refund_fee'] = 0;
        $list['product_total_fee'] = 0;
        $list['product_refund_fee'] = 0;
        foreach ($order as $k =>$v){
            if($v['class'] == 'recharge') {
                $list['recharge_total_fee'] += $v['total_fee'];
                $list['recharge_refund_fee'] += $v['refund_fee'];
                $order[$k]['brand_name'] = '充值';
                $order[$k]['create_time'] = $v['create_time'];
            }else{
                foreach ($v['detail'] as $kk =>$vv){
                    $labelinfo = db($v['class'].'_label')->where("label_id in ($vv[labels])")->field('cn')->select();
                    foreach ($labelinfo as $item) {
                        isset($order[$k]['detail'][$kk]['label_name'])?$order[$k]['detail'][$kk]['label_name'] .= ','.$item['cn']:$order[$k]['detail'][$kk]['label_name'] = $item['cn'];
                    }
                    $order[$k]['detail'][$kk]['total_fee'] = $vv['price'];
                    $order[$k]['detail'][$kk]['brand_name'] = $vv['name'];
                    $order[$k]['detail'][$kk]['create_time'] = $v['create_time'];
                }
                $list['product_total_fee'] +=$v['total_fee'];
                $list['product_refund_fee'] +=$v['refund_fee'];
            }
            if(isset($v['detail'])) {
                isset($list['list'])?$list['list'] = array_merge($list['list'],$order[$k]['detail']):$list['list'] = $order[$k]['detail'];
            }else{
                isset($list['list'])?$list['list'][]=$order[$k]:$list['list'][] = $order[$k];
            }
        }
        if(!$list) return array('state'=>1,'data'=>[]);
        if(isset($list['list'])) {
            array_multisort(array_column($list['list'], 'create_time'), SORT_DESC, $list['list']);
            foreach ($list['list'] as $k => $v) {
                $list['list'][$k]['create_time'] = date('Y-m-d', $v['create_time']);
            }
        }
        return [
            'state' => 1,
            'data' => ['info' => $list]
        ];
    }
    //客户充值记录
    public function uesrRecharge($filter){
        if (!$filter['user_id']){
            return ['state'=>0,'errormsg'=>'not find user_id','errorcode'=>'client uesrRecharge101'];
        }
        $where = "";
        if($filter['start'] && $filter['end']){
            $startime = strtotime($filter['start']);
            $endtime = strtotime($filter['end']);
            $where = "o.create_time>".$startime." and o.create_time<".$endtime;
        }
        if (isset($filter['page']) && $filter['page'] != '') {
            $pages = $filter['page'] - 1;
        } else {
            $pages = 0;
        }
        if (isset($filter['maxPerPage']) && $filter['maxPerPage'] != '') {
            $maxPerPage = $filter['maxPerPage'];
        } else {
            $maxPerPage = 12;
        }
        $recharge = Db::name('pay')
            ->alias('p')
            ->join('order o','o.order_id = p.order_id')
            ->where("o.class = 0 and o.user_id=".$filter['user_id'])
            ->where($where)
            ->field("o.order_id,p.total_fee,o.create_time")
            ->limit($pages*$maxPerPage,$maxPerPage)
            ->select();
        foreach ($recharge as $k=>$v){
            //$recharge[$k]['time'] = date('Y-m-d H:i:s',$v['create_time']);
            $pay_refund = Db::name('pay_refund')->where("order_id = $v[order_id]")->select();
            $refund = [];
            foreach ($pay_refund as $key=>$value){
                $refund[] = $value['refund_fee'];
            }
            $recharge[$k]['refund_fee'] = array_sum($refund);
        }
        if (!$recharge){
            return ['state'=>0,'errormsg'=>'没有充值记录','errorcode'=>'client uesrRecharge102'];
        }
        $pages = $pages + 1;
        $recharge_type = Db::name('pay')
            ->alias('p')
            ->join('order o','o.order_id = p.order_id')
            ->where("o.class = 0 and o.user_id=".$filter['user_id'])
            ->where($where)
            ->field("o.order_id,p.total_fee,o.create_time")
            ->limit($pages*$maxPerPage,$maxPerPage)
            ->select();
        if (count($recharge_type)){
            $more = 1;
        }else{
            $more = 0;
        }
        return array('state' => 1, 'data' => array('list' => $recharge, 'more' => $more, 'maxPerPage' => $maxPerPage));

    }

    //获取会员资格
    public function memberList()
    {
        if(!model('admin')->is('client')['state']) return ['state'=>0,'errormsg'=>'您没有权限'];
        if (isset($filter['page']) && $filter['page'] != '') {
            $pages = $filter['page'];
        } else {
            $pages = 1;
        }
        $userlist = db('user')
            ->alias('u')
            ->join('user_tel t','u.user_id = t.user_id')
            ->field('u.user_id,create_time,nickname,set_name,tel')
            ->where("t.state = 1")
            ->paginate(12,false,['page'=>$pages]);
        $page = $userlist->render();
        $userlist = $userlist->items();
        $page=preg_replace("(<a[^>]*page[=|/](\d+).+?>(.+?)<\/a>)","<a href='javascript:ajax_page($1);'>$2</a>",$page);
        $page=str_replace("&laquo;","上一页",$page);
        $page=str_replace("&raquo;","下一页",$page);

        if(!$userlist) return ['state'=>1,'data'=>[]];
        foreach ($userlist as $k => $v){
            //查询积分
            $points = db('forum_points')->where("user_id = $v[user_id]")->order('create_time desc')->select();
            if(!$points){
                $userlist[$k]['points'] = 0;
                $amount = 0;
            }else{
                foreach ($points as $kk =>$vv){
                    isset($amount)?$amount +=$vv['amount']:$amount = $vv['amount'];
                }
                $userlist[$k]['create_time'] = $points[0]['create_time'];
            }
            $userlist[$k]['points'] = $amount;
            //判断等级
            if($amount<50){
                $userlist[$k]['empirical_value'] = 'LV1';
            }elseif($amount>=50 && $amount<300){
                $userlist[$k]['empirical_value'] = 'LV2';
            }elseif (($amount>=300 && $amount<500)){
                $userlist[$k]['empirical_value'] = 'LV3';
            }elseif ($amount>=500 && $amount<5000){
                $userlist[$k]['empirical_value'] = 'LV4';
            }elseif ($amount>=5000){
                $userlist[$k]['empirical_value'] = 'LV5';
            }
            $userlist[$k]['user_name'] = $v['set_name']?userTextDecode($v['set_name']):userTextDecode($v['nickname']);
        }
        array_multisort(array_column($userlist,'create_time'),SORT_DESC,$userlist);
        return ['state'=>1,'data'=>['list'=>$userlist],'page'=>$page];
    }
    //获取会员信息
    public function userList($field){
        if (!$field){
            return [
                'state' => 0,
                'errormsg' => 'field error'
            ];
        }
        if (isset($field['page'])&&$field['page'] != '') {
            $pages = $field['page'];
        } else {
            $pages = 1;
        }
        /*if(preg_match("/^1[34578]\d{9}$/", $field['keywords'])){
            $where = "EXISTS(SELECT * FROM ".$this->prefix."user_tel WHERE user_id=".$this->prefix."user.user_id AND state=1 AND tel=$field[keywords])";
        }elseif(is_numeric($field['keywords']) && (strlen($field['keywords'])>=8 && strlen($field['keywords'])<=20)){
            $where = "EXISTS(SELECT * FROM ".$this->prefix."user_card WHERE user_id=".$this->prefix."user.user_id AND state=1 AND card_code=$field[keywords])";
        }elseif($field['keywords']!=''){
            $where = keywordSQL($field['keywords'],'nickname,real_name,set_name');
        }*/
        if(isset($field['keywords'])&& $field['keywords']!=""){
            $user = model('user')->exist(['tel'=>$field['keywords']]);
            if($user['state']){
                $where = "t.user_id=".$user['data']['user_id'];
            }else{
//                $card = model('card')->getUserIdByCode($field['keywords']);
//                if($card['state']){
//                    $where = "t.user_id=".$card['data']['user_id'];
//                }else{
                $where = keywordSQL($field['keywords'],'nickname,real_name,set_name');
//                }
            }
        }
        if(isset($where)) {
            $user = Db::name('user')
                ->alias('u')
                ->join('user_tel t','u.user_id = t.user_id')
                ->where('t.state = 1')
                ->where($where)
                ->order('create_time desc')
                ->paginate(12,false,['page'=>$pages]);
        }else{
            $user = Db::name('user')
                ->alias('u')
                ->join('user_tel t','u.user_id = t.user_id')
                ->where('t.state = 1')
                ->order('create_time desc')
                ->paginate(12,false,['page'=>$pages]);
        }
        $page = $user->render();
        $user = $user->items();
        $page=preg_replace("(<a[^>]*page[=|/](\d+).+?>(.+?)<\/a>)","<a href='javascript:ajax_page($1);'>$2</a>",$page);
        $page=str_replace("&laquo;","上一页",$page);
        $page=str_replace("&raquo;","下一页",$page);
        //基本信息
        $member = [];
        if (!empty($user)){
            foreach ($user as $k=>$v){
                $userinfo = model('User')->getInfoById($v['user_id']);
                if ($userinfo){
                    $member[$k]['user_name'] = $userinfo['set_name'] ? $userinfo['set_name']:$userinfo['user_name'];
                    $member[$k]['tel'] = isset($tel['tel']) ? $tel['tel']:'';
                    $member[$k]['headimgurl'] = isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '';
                    $member[$k]['user_id'] = $userinfo['user_id'];
                    $member[$k]['create_time'] = $v['create_time'];
                    $member[$k]['tel'] = $v['tel'];
                    if($v['admin_user_id']){
                        $admininfo = model('User')->getInfoById($v['admin_user_id']);
                        $member[$k]['admin_user_name'] = $admininfo['set_name'] ? $admininfo['set_name']:$admininfo['user_name'];
                    }else{
                        $member[$k]['admin_user_name'] = '';
                    }
                    $member[$k]['balance'] = model('Account')->getBalanceByUserId($v['user_id'])['data']['balance'];
                }

                //查询积分
                $points = db('forum_points')->where("user_id = $v[user_id]")->order('create_time desc')->select();
                if(!$points){
                    $userlist[$k]['points'] = 0;
                    $amount = 0;
                }else{
                    foreach ($points as $kk =>$vv){
                        isset($amount)?$amount +=$vv['amount']:$amount = $vv['amount'];
                    }
                    $member[$k]['create_time'] = $points[0]['create_time'];
                }
                $member[$k]['points'] = $amount;
                //判断等级
                if($amount<50){
                    $member[$k]['empirical_value'] = 'LV1';
                }elseif($amount>=50 && $amount<300){
                    $member[$k]['empirical_value'] = 'LV2';
                }elseif (($amount>=300 && $amount<500)){
                    $member[$k]['empirical_value'] = 'LV3';
                }elseif ($amount>=500 && $amount<5000){
                    $member[$k]['empirical_value'] = 'LV4';
                }elseif ($amount>=5000){
                    $member[$k]['empirical_value'] = 'LV5';
                }
            }
        }
        if(!$member){
            return [
                'state' =>1,
                'data' => []
            ];
        }

        return [
            'state'=> 1,
            'data'=>['list'=>$member],
            'page'=>$page
        ];
    }
//用户生成充值订单
    public function orderCreateByUser($total_fee){
        if(!model('user')->getUserId()){
            return [
                'state' => 0,
                'errormsg' => 'not find user exist'
            ];
        }
        //检查total_fee
        if (!is_numeric($total_fee) || $total_fee <= 0) {
            return [
                'state' => 0,
                'errormsg' => 'total_fee error'
            ];
        }
        if (!$total_fee){
            return [
                'state' => 0,
                'errormsg' => 'need total_fee'
            ];
        }
//        $member = model('member')->getInfoByIdUserId($this->userId);
//        if(!$member['state']){
//            return ['state'=>0,'errormsg'=>'不能充值'];
//        }
//        if($member['data'][0]['key']=='vip2017'){
//            if($total_fee<50000){
//                return ['state'=>0,'errormsg'=>'至少充值500'];
//            }
//        }
//        if($member['data'][0]['key']=='cluber2017'){
//            if($total_fee<20000){
//                return ['state'=>0,'errormsg'=>'至少充值200'];
//            }
//        }
        $demand = 'recharge';
        $config = getConfig("product");
        $data['class'] = $config['recharge']['id'];
        $data['user_id'] = model('user')->getUserId();
        $data['state'] = 1;
        $data['create_time'] = $_SERVER['REQUEST_TIME'];
        $data['total_fee'] = $total_fee;
        $ret = model('Order')->add($demand, $data);
        return $ret;
    }
    //账户充值财务报表
    public function report($data)
    {
        header("Content-type:application/vnd.ms-excel;charset:UTF-8");
        $name = "weyard_" . "client";
        if ($data['start'] && $data['end']) {
            $startime = strtotime($data['start']);
            $endtime = strtotime($data['end']);
            if ($startime == $endtime) {
                $filename = $name . "_finance" . " '" . $data['start'] . "' " . date('Y-m-d H:i:s', time());
            } else {
                $filename = $name . "_finance" . " '" . $data['start'] . '--' . $data['end'] . "' " . date('Y-m-d H:i:s', time());
            }
        } else {
            $filename = $name . "_finance汇总" . date('Y-m-d H:i:s', time());
        }
        header("Content-Disposition:attachment;filename=" . $filename . ".xls"); //告诉浏览器输出的文件名
        header('Cache-Control: max-age=0'); //禁止缓存
        header("Pragma: no-cache");
        header("Expires: 0");
        //找到order里的cafe订单
        $where = "o.class = 0";
        //时间区间判断
        if (isset($startime) && isset($endtime) && $startime && $endtime) {
            if ($startime == $endtime) {
                $endtime = $startime + 86400;
                $where .= " and p.create_time > $startime and p.create_time < $endtime";
            } else {
                $endtime = $endtime + 86399;
                $where .= " and p.create_time > $startime and p.create_time < $endtime";
            }
        }
        if ($data['code'] == 'utf-8') {
            $order_id = "订单号";
            $refund_total_fee = "退款金额";
            $create_time = "创建时间";
            $total_fee = "成交价";
            $user_name = "用户名";
            $admin_user_name = "操作员";
            $channel = "支付方式";
            $cashpay = "现金支付";
            $accountpay = "会员支付";
            $wxpay = "微信支付";
            $alipay = "支付宝支付";
        } else {
            $order_id = iconv("UTF-8", "GBK//IGNORE", "订单号");
            $refund_total_fee = iconv("UTF-8", "GBK//IGNORE", "退款金额");
            $create_time = iconv("UTF-8", "GBK//IGNORE", "创建时间");
            $total_fee = iconv("UTF-8", "GBK//IGNORE", "成交价");
            $user_name = iconv("UTF-8", "GBK//IGNORE", "用户名");
            $admin_user_name = iconv("UTF-8", "GBK//IGNORE", "操作员");
            $channel = iconv("UTF-8", "GBK//IGNORE", "支付方式");
            $cashpay = iconv("UTF-8", "GBK//IGNORE", "现金支付");
            $accountpay = iconv("UTF-8", "GBK//IGNORE", "会员支付");
            $wxpay = iconv("UTF-8", "GBK//IGNORE", "微信支付");
            $alipay = iconv("UTF-8", "GBK//IGNORE", "支付宝支付");
        }
        $order = 'p.create_time desc';
        $files = "o.order_id,o.class,o.user_id,o.admin_user_id,o.total_fee,o.total_fee_original,p.create_time,p.pay_id,p.channel";
        $order_info = db('order')->alias('o')->join('pay p', 'p.order_id = o.order_id')->field($files)
            ->where($where)->order($order)->select();
        foreach ($order_info as $k => $v) {
            //订单原价
            $order_info[$k]['total_fee'] = $v['total_fee'] / 100;
            //订单退款金额
            $order_id = $v['order_id'];
            $pay_refund = Db::name('pay_refund')->field('refund_fee')->where("order_id = '$order_id'")->select();
            if ($pay_refund) {
                $refund_total = '';
                foreach ($pay_refund as $k_n => $v_n) {
                    $refund_total += $v_n['refund_fee'];
                }
                $order_info[$k]['refund_total_fee'] = round($refund_total / 100, 2);
            } else {
                $order_info[$k]['refund_total_fee'] = 0;
            }
            //订单user_name
            $user_info = model('User')->getInfoById($v['user_id']);
            $order_info[$k]['user_id'] = $v['user_id'];
            $user_info['user_name'] = isset($user_info['set_name']) ? $user_info['set_name'] : userTextDecode($user_info['user_name']);
            if ($data['code'] != 'utf-8') {
                $order_info[$k]['user_name'] = iconv("UTF-8", "GBK//IGNORE", $user_info['user_name']);
            } else {
                $order_info[$k]['user_name'] = $user_info['user_name'];
            }
            //订单admin_user_name
            $admin_user_info = model('User')->getInfoById($v['admin_user_id']);
            $order_info[$k]['admin_user_id'] = $v['admin_user_id'];
            $admin_user_info['admin_user_name'] = $admin_user_info['set_name'] ? $admin_user_info['set_name'] : $admin_user_info['user_name'];
            if ($data['code'] != 'utf-8') {
                $order_info[$k]['admin_user_name'] = iconv("UTF-8", "GBK//IGNORE", $admin_user_info['admin_user_name']);
            } else {
                $order_info[$k]['admin_user_name'] = $admin_user_info['admin_user_name'];
            }
            //支付渠道
            switch ($v['channel']) {
                case 1:
                    $order_info[$k]['channel'] = $cashpay;
                    break;
                case 2:
                    $order_info[$k]['channel'] = $accountpay;
                    break;
                default:
                    switch ((int)($v['channel'] / 100)) {
                        case 1:
                            $order_info[$k]['channel'] = $wxpay;
                            break;
                        case 2:
                            $order_info[$k]['channel'] = $alipay;
                            break;
                    }
            }

            //时间
            $order_info[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
        }
        $order = [];
        foreach ($order_info as $k => $v) {
            $order[$k]['order_id'] = $v['order_id'];
            $order[$k][$create_time] = $v['create_time'];
            $order[$k][$total_fee] = $v['total_fee'];
            $order[$k][$refund_total_fee] = $v['refund_total_fee'];
            $order[$k][$user_name] = $v['user_name'];
            $order[$k][$admin_user_name] = $v['admin_user_name'];
            $order[$k][$channel] = $v['channel'];
        }
        //数据title
        $d = array();
        foreach ($order as $k => $v) {
            foreach ($v as $kk => $vv) {
                $d[$kk] = strtoupper($kk);
            }
            break;
        }
        //合并数据
        array_unshift($order, $d);
        if (!empty($order)) {
            foreach ($order as $key => $val) {
                $order[$key] = implode("\t", str_replace("\t", " ", $order[$key]));
            }
            echo implode("\n", $order);
        }
    }
}